home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 4 / FM Towns Free Software Collection 4 - Disc 1.iso / oh_towns / ccheck / chk.c next >
C/C++ Source or Header  |  1991-10-18  |  11KB  |  387 lines

  1. /*    chk -- C program check sum    */
  2.  
  3. /* 御使用のコンパイラにあわせて       */
  4. /* 次の4つのうちから、ひとつを選んで */
  5. /* 注釈の外に出して下さい。           */
  6. /* #define DRACO */ /*  FM-7/AV系 Draco-C      */
  7. /* #define MWC   */ /*  OS-9/6809 Microware-C  */
  8. #define MSC         /*  MS-DOS    Micro-Soft C */
  9. /* #define RR */    /*  Runser    Draco-C      */
  10.  
  11. #ifdef  RR
  12.   /*    Runser    */
  13. # include "rusio.c"
  14. # include "string.c"
  15. # include "chklib.c"
  16. # define PRINTER stdprt
  17. # define errno 0
  18. #else
  19. # ifdef DRACO
  20.    /*    Draco C    */
  21. #  include "0:fileio.c"
  22. #  include "0:string.c"
  23. #  define PRINTER stdprt
  24. #  define errno 0
  25. # else
  26.    /*    MW-C & MS-C    */
  27. #  include <stdio.h>
  28. #  include <ctype.h>
  29. #  ifdef MWC
  30.     /*    MW-C    */
  31. #   define PRINTER stderr
  32. #  else
  33.     /*    MS-C    */
  34. #   define PRINTER stdprn
  35. #   define errno 0
  36. #  endif
  37. # endif
  38. #endif
  39.  
  40. #ifndef RR
  41. # define ERR  (-1)
  42. #endif
  43. #define YES    1
  44. #define NO     0
  45. #define NULL   0
  46.  
  47. #define BUF_LEN 256
  48.  
  49. FILE *out ;
  50.  
  51. char buf [ BUF_LEN ];
  52. int  list = NO, numb = NO,
  53.      check = NO,
  54.      file = NO ;
  55.  
  56. main( argc, argv )
  57.     int  argc;
  58.     char **argv;
  59. {
  60.     FILE *fp, *fopen();
  61.  
  62.     out = stdout ;
  63.  
  64.     if ( option( argc, argv ) < 1 ) {
  65.         putc ( '\n', out );
  66.         checksum( stdin, out );
  67.     } else {
  68.         while ( --argc ) {
  69.             if ( **++argv != '-' ) {
  70.                 if ( ( fp = fopen( *argv , "r" ) ) != NULL ) {
  71.                     if ( !check && !list && !numb )
  72.                         fprintf( out, "\n    file <%s>\n\n", *argv );
  73.                     checksum( fp, out );
  74.                     fclose( fp );
  75.                 } else {
  76.                     exit( errno );
  77.                 }
  78.             }
  79.         }
  80.     }
  81.     if ( file )
  82.         fclose( out );
  83. }
  84.  
  85. option( argc, argv )
  86.     int  argc;
  87.     char **argv;
  88. {
  89.     char c, *p, fname[256];
  90.     int  count, print;
  91.  
  92.     count = 0;    print = NO;
  93.  
  94.     while ( --argc )
  95.         if ( *(p=*++argv) == '-' )
  96.             while ( c = *++p ) {
  97.                 switch( c ) {
  98.                 case 'f':
  99.                 case 'F':
  100.                     file = YES;
  101.                     if ( *++p != '=' ) {
  102.                       prterr( "\n <-f> option format is illegal.\n" );
  103.                       prterr( "   -f=<file name> (ex. -f=1:chk.c)\n\n" );
  104.                       exit( 0 );
  105.                     } else
  106.                       strcpy( fname, ++p );
  107.                     break;
  108.                 case 'p':
  109.                 case 'P':   print = YES;   break;
  110.                 case 'n':
  111.                 case 'N':   numb=YES; list=NO; check=NO;   break;
  112.                 case 'l':
  113.                 case 'L':   list=YES; numb=NO; check=NO;   break;
  114.                 case 'c':
  115.                 case 'C':   check=YES; list=NO; numb=NO;   break;
  116.                 case '?':   usage();
  117.                 default:
  118.                     fprintf( stderr, "\nunknown option: -%c\n\n", c );
  119.                     exit(0);
  120.                 }
  121.                 if ( c == 'f' || c == 'F' )
  122.                     break;
  123.             }
  124.         else
  125.             ++count;
  126.  
  127.     if ( file && print ) {
  128.         prterr( "\n You must choose between -f and -p option.\n\n" );
  129.         exit(0);
  130.     } else
  131.         if ( print )
  132.             out = PRINTER;
  133.         else
  134.             if ( file && ( out = fopen( fname, "w" ) ) == NULL )
  135.                 exit( errno );
  136.  
  137.     return( count );
  138. }
  139.  
  140. usage()
  141. {
  142.     prterr( "\n" );
  143.     prterr( "usage:  chk [opt] [<file name>] [opt]\n" );
  144.     prterr( "  opt: -f=filename   output for <filename>\n" );
  145. #ifdef MWC
  146.     prterr( "       -p   output for standard error path\n" );
  147. #else
  148.     prterr( "       -p   output for printer\n" );
  149. #endif
  150.     prterr( "       -l   printout text (without line number)\n" );
  151.     prterr( "       -n   printout line number + text\n" );
  152.     prterr( "       -c   printout line number + check sum + text\n" );
  153.     prterr( "       -?   You know this one.\n" );
  154.     prterr( "\n" );
  155.     exit(0);
  156. }
  157.  
  158. prterr( s )
  159.     char *s;
  160. {    fprintf( stderr, s );    }
  161.  
  162. eor( ch, pos )
  163.     char ch;    int pos;
  164. {    return( ch ^ pos );    }
  165.  
  166. low( i )
  167.     int i;
  168. {    return( i & 0x00FF );    }
  169.  
  170. checksum( fp )
  171.     FILE *fp;
  172. {
  173.     char c, next_c, last_c, qc, chsum,
  174.          token[256], left[20];
  175.  
  176.     int  i, j, cpos, tpos, lmod,
  177.          lsum, xsum, ysum[10],
  178.          line, in_rem, in_asm, in_quote;
  179.  
  180.     line = 100;    in_rem = in_asm = in_quote = NO;
  181.  
  182.  
  183.     if ( list || numb ) {
  184.         while ( lread( fp ) != ERR ) {
  185.             if ( numb )
  186.                 fprintf( out, "%3d0 '", ++line );
  187.             fprintf( out, "%s\n", buf );
  188.         }
  189.     } else {
  190.         do {
  191.             for ( i = 0 ; i < 10 ; i++ )
  192.                 ysum[i] = 0;    xsum = 0;
  193.  
  194.             for ( i = 0 ; i < 100 ; i ++ ) {
  195.                 if ( ( cpos = lread( fp ) ) == ERR )
  196.                     break;
  197.  
  198.                 ++ line;    tpos = chsum = 0;
  199.                 in_quote = NO;
  200.  
  201.                 if ( !check ) {
  202.                     if ( i == 0 ) {
  203.                         fprintf( out,
  204.                             "  line  10 20 30 40 50 60 70 80 90 00  Sum\n" );
  205.                         fprintf( out,
  206.                             " -----  -- -- -- -- -- -- -- -- -- --   --\n" );
  207.                     }
  208.  
  209.                     if ( (lmod = i % 10) == 0 ) {
  210.                         fprintf( out, "%5d0  ", line );
  211.                         get_l( cpos, left );
  212.                     }
  213.                 }
  214.  
  215.                 /*    in-line assembler exit ?    */
  216.                 if ( ! in_rem && buf[cpos] == '#' ) {
  217.                     get_token( cpos, token );
  218.                     if ( strcmp( token, "#endasm" ) == 0 )
  219.                         in_asm = NO;
  220.                 } else
  221.                     *token = '\0';
  222.  
  223.                 if ( in_asm ) {
  224.                     /*    in-line assembler source    */
  225.  
  226.                     if ( buf[cpos] != '*' ) {
  227.                         last_c = 0;
  228.                         while ( ( c = buf[cpos++] ) ) {
  229.  
  230.                             if ( !in_quote && isspace(last_c) && c == ';' )
  231.                                 break;
  232.  
  233.                             if ( c == '"' || c == '/' ) {
  234.                                 if ( in_quote ) {
  235.                                     if ( qc == c )
  236.                                         in_quote = NO;
  237.                                 } else {
  238.                                     if ( isspace(last_c) ) {
  239.                                         in_quote = YES;
  240.                                         qc = c;
  241.                                     }
  242.                                 }
  243.                             }
  244.  
  245.                             if ( in_quote || ! isspace( c ) ) {
  246.                                 chsum += eor( c, ++tpos );
  247.                             }
  248.                             last_c = c;
  249.                         }
  250.                     }
  251.                 } else {
  252.                     /*    in-line assembler in ?    */
  253.                     if ( strcmp( token, "#asm" ) == 0 )
  254.                         in_asm = YES;
  255.  
  256.                     /*    C program    */
  257.  
  258.                     while ( ( c = buf[cpos] ) ) {
  259.                         next_c = buf[++cpos];
  260.  
  261.                         if ( in_rem ) {
  262.                             if ( c == '*' && next_c == '/' ) {
  263.                                 in_rem = NO;    ++cpos;
  264.                             }
  265.                         } else {
  266.                             if ( c == '/' && next_c == '*' ) {
  267.                                 in_rem = YES;   ++cpos;
  268.                             } else {
  269.                                 if ( in_quote ) {
  270.                                     if ( c == '\\' ) {
  271.                                         chsum += eor( c, ++tpos );
  272.                                         c = buf[cpos++];
  273.                                     } else if ( qc == c ) {
  274.                                         in_quote = NO;
  275.                                     }
  276.                                 } else {
  277.                                     if ( c == '"' || c == '\'' ) {
  278.                                         in_quote = YES;
  279.                                         qc = c;
  280.                                     }
  281.                                 }
  282.  
  283.                                 if ( in_quote || ! isspace( c ) ) {
  284.                                     chsum += eor( c, ++tpos );
  285.                                 }
  286.                             }
  287.                         }
  288.                     }
  289.                 }
  290.  
  291.                 lsum = low( chsum );
  292.                 if ( check ) {
  293.                     fprintf( out, "%3d0 '0", line );
  294.                     fprintf( out, (tpos) ? "%02x":"--", lsum );
  295.                     fprintf( out, " '%s\n", buf );
  296.                 } else {
  297.                     fprintf( out, (tpos) ? "%02x ":"-- ", lsum );
  298.  
  299.                     xsum += lsum;
  300.                     ysum[lmod] += lsum;
  301.  
  302.                     if ( lmod == 9 ) {
  303.                         fprintf( out, ": %02x  <%s>\n", low( xsum ), left );
  304.                         xsum = 0;
  305.                     }
  306.                 }
  307.             }
  308.  
  309.             if ( !check ) {
  310.                 if ( lmod < 9 ) {
  311.                     for ( j = lmod + 1 ; j < 10 ; j ++ )
  312.                         fprintf( out, "   " );
  313.                     fprintf( out, ": %02x  <%s>\n", low( xsum ), left );
  314.                 }
  315.  
  316.                 if ( i > 0 ) {
  317.                     fprintf( out,
  318.                         " -----  ----------------------------------\n" );
  319.                     fprintf( out,"        " );
  320.  
  321.                     xsum = 0;
  322.                     for ( j = 0 ; j < 10 ; j ++ ) {
  323.                         fprintf( out, "%02x ", low( ysum[j] ) );
  324.                         xsum += ysum[j];
  325.                     }
  326.                     fprintf( out, "/ %02x\n\n", low( xsum ) );
  327.                 }
  328.             }
  329.         } while ( cpos >= 0 );
  330.     }
  331. }
  332.  
  333. lread( fp )
  334.     FILE *fp;
  335. {
  336.     char *s;
  337.     int  pos;
  338.  
  339.     if ( fgets( buf, BUF_LEN, fp ) == NULL )
  340.         return( ERR );
  341.  
  342.     /*    trimming last spaces    */
  343.     if ( (pos = strlen( buf )) > 0 ) {
  344.         while ( --pos >= 0 && isspace(buf[pos]) )
  345.             buf[pos] = '\0';
  346.     }
  347.  
  348.     /*    split line number    */
  349.     while ( 1 ) {
  350.         pos = 0;    s = buf;
  351.  
  352.         while ( *s && isspace( *s ) ) { s++;  pos++; }
  353.  
  354.         if ( isdigit( *s ) ) {
  355.             while ( *s && *s++ != '\'' ) ;
  356.             strcpy( buf, s );
  357.         } else
  358.             break;
  359.     }
  360.  
  361.     return( pos );
  362. }
  363.  
  364. get_l( pos, s )
  365.     int pos;
  366.     char *s;
  367. {
  368.     int i;    i = 0;
  369.  
  370.     while ( (*s++ = buf[ pos++ ]) && (i++)<16 ) ;
  371.  
  372.     *s = '\0';
  373. }
  374.  
  375. get_token( pos, token )
  376.     int  pos;
  377.     char *token;
  378. {
  379.     while ( buf[pos] && isspace( buf[pos] ) )
  380.         ++pos;
  381.     while ( buf[pos] && !isspace( buf[pos] ) )
  382.         *token++ = buf[pos++];
  383.     *token = '\0';
  384.  
  385.     return ( pos );
  386. }
  387.